package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.OrderDeliveryDto;
import com.itheima.dto.OrderPageDto;
import com.itheima.dto.OrderReceiverDto;
import com.itheima.manager.OmsOrderManager;
import com.itheima.dto.OrderMoneyDto;
import com.itheima.vo.OrderDetailsVo;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private OmsOrderManager omsOrderManager;

    /**
     * 订单分页查询和条件查询
     */
    @GetMapping("/list")
    public R findOrderList(OrderPageDto orderPageDto){
        PageBeanVo vo = omsOrderManager.findOrderList(orderPageDto);
        return R.success(vo);
    }

    /**
     * 修改订单费用信息
     */
    @PostMapping("/update/moneyInfo")
    public R updateMoneyInfo(@RequestBody OrderMoneyDto dto) {
        omsOrderManager.updateOrderMoney(dto);
        return R.success("修改成功!");
    }

    /**
     * 修改收货人信息
     */
    @PostMapping("/update/receiverInfo")
    public R updateReceiverInfo(@RequestBody OrderReceiverDto dto) {
        omsOrderManager.updateOrderReceiver(dto);
        return R.success("修改成功!");
    }

    /**
     * 批量删除订单(逻辑删除，将delete_status字段值改为1)
     */
    @PostMapping("/delete")
    public R delete(@RequestParam List<Integer> ids) {
        Integer count = omsOrderManager.delete(ids);
        if (count == 0) {
            return R.error("关闭订单失败!");
        }
        return R.success("删除成功!");
    }

    /**
     * 批量关闭订单
     */
    @PostMapping("/update/close")
    public R close(@RequestParam Map map) {
        omsOrderManager.close(map);
        return R.success("关闭订单成功!");
    }

    /**
     * 批量发货
     */
    @PostMapping("/update/delivery")
    public R delivery(@RequestBody List<OrderDeliveryDto> list) {
        omsOrderManager.delivery(list);
        return R.success("发货成功!");
    }

    /**
     * 根据订单id获取订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findOrderDetails(@PathVariable Integer id) {
        OrderDetailsVo vo = omsOrderManager.findById(id);
        return R.success(vo);
    }

    /**
     * 备注订单
     */
    @PostMapping("/update/note")
    public R note(@RequestParam Map<String, String> map) {
        Integer count = omsOrderManager.note(map);
        if (count == 0) {
            return R.error("备注失败!");
        }
        return R.success("备注成功!");
    }
}
